﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClientRegister
{
    public partial class RemoveClassOfStudentForm : Form
    {
        public RemoveClassOfStudentForm()
        {
            InitializeComponent();
        }

        DataSet ds;

        SynchronizeData sync = new SynchronizeData();

        private void RemoveClassOfStudentForm_Load(object sender, EventArgs e)
        {
            ////Load du lieu tu server
            //sync.GetDataForInsertMethodWithID(sync.studentTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.studentTb, sync.updatecol);

            ////Load du lieu khoa hoc
            //sync.GetDataForInsertMethodWithID(sync.CourTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.CourTb, sync.updatecol);

            ////Load du lieu lop hoc
            //sync.GetDataForInsertMethodWithID(sync.ClassTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.ClassTb, sync.updatecol);

            ////Load du lieu lop hoc cua hoc vien
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);
            //sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);
        }

        private void cbbxCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbxCourse.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                ClassDataProvider cls = new ClassDataProvider();
                ds = cls.loadClassOfStudent(txtID.Text.ToString(), cbbxCourse.SelectedValue.ToString());
                GridListClass.DataSource = ds.Tables[0];
            }
        }

        private void txtID_TextChanged(object sender, EventArgs e)
        {
            CourseDataProvider course = new CourseDataProvider();
            if (txtID.Text != "")
            {
                DataSet ds = course.loadCoursesOfStudent(txtID.Text.ToString());
                DataTable dt = ds.Tables[0];
                cbbxCourse.DataSource = dt;
                cbbxCourse.DisplayMember = "NameCourse";
                cbbxCourse.ValueMember = "ID";
            }
            else
            {
                cbbxCourse.DataSource = null;
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (GridListClass.SelectedCells.Count > 0)
            {

                string IDStu = txtID.Text.ToString();
                string IDClass = GridListClass.CurrentRow.Cells["ID"].Value.ToString();

                StudentInClassDataProvider remo = new StudentInClassDataProvider();

                string xml = remo.selectClassOfStu(IDStu, IDClass);
                
                int result = remo.RemoveStudentFromClass(IDStu, IDClass);
                
                if(result == 1)
                {
                    sync.transfDataForDeleteMethod(xml, sync.ClassOfStu);
                    MessageBox.Show("Đã xóa");
                }
                
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
